//
//  GLTexturePVRTC.m
//  DeferredLighting
//
//  Created by Holmes Futrell on 2/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GLTexturePVRTC.h"


@implementation GLTexture(PVRTC)
#if TARGET_OS_IPHONE
-(id)initWithPVRTC:(NSString *)file size:(int)size bpp:(unsigned int)bpp flags:(unsigned int)flags {
	
	self = [super init];
	
	width = size;
	height = size;
	
	internalWidth = width;
	internalHeight = height;
	
	flags = [self enableDefaultFlags: flags];
	
	/* check texture format */
	GLuint format;
	if (bpp == 2) {
		if (flags & kGLTextureNoAlphaFlag) {
			format = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
		}
		else {
			format = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
		}
	}
	else if (bpp == 4) {
		if (flags & kGLTextureNoAlphaFlag) {
			format = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
		}
		else {
			format = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
		}
	}
	else {
		NSLog(@"Error, PVRTC bpp must be 4 or 2");
		return nil;
	}
	
	glGenTextures(1, &descriptor);
	glBindTexture(GL_TEXTURE_2D, descriptor);
	[self setTextureModes: flags];
	
	NSData *data = [NSData dataWithContentsOfFile: file];
	if (data == nil) {
		NSLog(@"Error, could not open %@ for reading PVRTC!", file);
		return nil;
	}
	
	glCompressedTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, [data length], [data bytes]); 
	
	return self;
	
}
#endif

@end

